package com.lx.service.impl;

import com.alibaba.dubbo.config.annotation.Reference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codingapi.tx.annotation.TxTransaction;
import com.lx.MsagesException;
import com.lx.Remessages;
import com.lx.Request;
import com.lx.Response;
import com.lx.constants.CommonConstants;
import com.lx.dal.entity.Goods;
import com.lx.dal.mapper.GoodsMapper;
import com.lx.demo.b.interfaces.TestDemoBDubboService;
import com.lx.demo.b.interfaces.dto.TestDemoBDubboServiceDTO;
import com.lx.demo.interfaces.TestDubboDemoService;
import com.lx.demo.interfaces.dto.TestDubboRequestDTO;
import com.lx.service.DemoService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.UUID;

/**
 * Created by lanyxp on 2021/2/24 15:52
 */
@Slf4j
@Service
public class DemoServiceImpl extends ServiceImpl implements DemoService {
    @Resource
    private GoodsMapper goodsMapper;

    @Reference(check = false, timeout = CommonConstants.DUBO_TIME, owner = CommonConstants.DUBO_OWNER,
            group = CommonConstants.DUBO_GROUP, version = CommonConstants.DUBO_VERSION)
    private TestDubboDemoService testDubboDemoService;

    @Reference(check = false, timeout = CommonConstants.DUBO_TIME, owner = CommonConstants.DUBO_OWNER,
            group = CommonConstants.DUBO_GROUP, version = CommonConstants.DUBO_VERSION)
    private TestDemoBDubboService testDemoBDubboService;

    /****
     * Transactional 由spring提供是事务控制本地事务
     * TxTransaction 分布式事务由TX-LCN提供isStart=true代表的是该服务是事务的发起方
     * @return Boolean
     */
    @Transactional(rollbackFor = Exception.class)
    @TxTransaction(isStart = true)
    @Override
    public boolean demoTestTransaction() {
        String code = UUID.randomUUID().toString();
        TestDubboRequestDTO requestDTO = new TestDubboRequestDTO();
        requestDTO.setCode(code);
        Request<TestDubboRequestDTO> dubboRequests = Remessages.CreateRequest(requestDTO);
        Response response = this.testDubboDemoService.addDataTest(dubboRequests);
        if (!response.isSuccess()) {
            throw new MsagesException("dubbo服务调用异常");
        }
        TestDemoBDubboServiceDTO dubboRequestB = new TestDemoBDubboServiceDTO();
        dubboRequestB.setName("lanyxp2");
        final Response dubbobResult = this.testDemoBDubboService.addDataTestB(Remessages.CreateRequest(dubboRequestB));
        if (!dubbobResult.isSuccess()) {
            throw new MsagesException("dubboB服务调用异常");
        }
        Object body = response.getBody();
        Goods entity = new Goods();
        entity.setGid(body.toString());
        entity.setCreateTime(new Date());
        entity.setUpdateTime(new Date());
        entity.setName("测试2");
        final int insert = this.goodsMapper.insert(entity);
        //测试分布式事务  本地事务回滚 dubbo远程服务回滚
        int a = 3 / 0;
        return true;
    }
}
